<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>field_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_editor_field_test.js.html">field_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Shared tests for goog.editor.Field and ContentEditableField.
<a name="line17"></a> * Since ContentEditableField switches many of the internal code paths in Field
<a name="line18"></a> * (such as via usesIframe()) it&#39;s important to re-run a lot of the same tests.
<a name="line19"></a> *
<a name="line20"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line21"></a> * @author jparent@google.com (Julie Parent)
<a name="line22"></a> * @author gboyer@google.com (Garrett Boyer)
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.editor.field_test&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line28"></a>goog.require(&#39;goog.editor.Field&#39;);
<a name="line29"></a>goog.require(&#39;goog.editor.Plugin&#39;);
<a name="line30"></a>goog.require(&#39;goog.editor.Command&#39;);
<a name="line31"></a>goog.require(&#39;goog.events&#39;);
<a name="line32"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line33"></a>goog.require(&#39;goog.functions&#39;);
<a name="line34"></a>goog.require(&#39;goog.testing.LooseMock&#39;);
<a name="line35"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line36"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line37"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line38"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line39"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line40"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line41"></a>goog.setTestOnly(&#39;Tests for goog.editor.*Field&#39;);
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/** Constructor to use for creating the field. Set by the test HTML file. */
<a name="line45"></a>var FieldConstructor;
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/** Hard-coded HTML for the tests. */
<a name="line49"></a>var HTML = &#39;&lt;div id=&quot;testField&quot;&gt;I am text.&lt;/div&gt;&#39;;
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>function setUp() {
<a name="line53"></a>  goog.dom.getElement(&#39;parent&#39;).innerHTML = HTML;
<a name="line54"></a>  assertTrue(&#39;FieldConstructor should be set by the test HTML file&#39;,
<a name="line55"></a>      goog.isFunction(FieldConstructor));
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>function tearDown() {
<a name="line60"></a>  // NOTE(nicksantos): I think IE is blowing up on this call because
<a name="line61"></a>  // it is lame. It manifests its lameness by throwing an exception.
<a name="line62"></a>  // Kudos to XT for helping me to figure this out.
<a name="line63"></a>  try {
<a name="line64"></a>    goog.events.removeAll();
<a name="line65"></a>  } catch (e) {}
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>// Tests for the plugin interface.
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Dummy plugin for test usage.
<a name="line74"></a> * @constructor
<a name="line75"></a> * @extends {goog.editor.Plugin}
<a name="line76"></a> */
<a name="line77"></a>function TestPlugin() {
<a name="line78"></a>  this.getTrogClassId = function() {
<a name="line79"></a>    return &#39;TestPlugin&#39;;
<a name="line80"></a>  };
<a name="line81"></a>
<a name="line82"></a>  this.handleKeyDown = goog.nullFunction;
<a name="line83"></a>  this.handleKeyPress = goog.nullFunction;
<a name="line84"></a>  this.handleKeyUp = goog.nullFunction;
<a name="line85"></a>  this.handleKeyboardShortcut = goog.nullFunction;
<a name="line86"></a>  this.isSupportedCommand = goog.nullFunction;
<a name="line87"></a>  this.execCommandInternal = goog.nullFunction;
<a name="line88"></a>  this.queryCommandValue = goog.nullFunction;
<a name="line89"></a>  this.activeOnUneditableFields = goog.nullFunction;
<a name="line90"></a>  this.handleSelectionChange = goog.nullFunction;
<a name="line91"></a>}
<a name="line92"></a>goog.inherits(TestPlugin, goog.editor.Plugin);
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Tests that calling registerPlugin will add the plugin to the
<a name="line97"></a> * plugin map.
<a name="line98"></a> */
<a name="line99"></a>function testRegisterPlugin() {
<a name="line100"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line101"></a>  var plugin = new TestPlugin();
<a name="line102"></a>
<a name="line103"></a>  editableField.registerPlugin(plugin);
<a name="line104"></a>
<a name="line105"></a>  assertEquals(&#39;Registered plugin must be in protected plugin map.&#39;,
<a name="line106"></a>      plugin, editableField.plugins_[plugin.getTrogClassId()]);
<a name="line107"></a>  assertEquals(&#39;Plugin has a keydown handler, should be in keydown map&#39;,
<a name="line108"></a>      plugin,
<a name="line109"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.KEYDOWN][0]);
<a name="line110"></a>  assertEquals(&#39;Plugin has a keypress handler, should be in keypress map&#39;,
<a name="line111"></a>      plugin,
<a name="line112"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.KEYPRESS][0]);
<a name="line113"></a>  assertEquals(&#39;Plugin has a keyup handler, should be in keuup map&#39;,
<a name="line114"></a>      plugin,
<a name="line115"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.KEYUP][0]);
<a name="line116"></a>  assertEquals(
<a name="line117"></a>      &#39;Plugin has a selectionchange handler, should be in selectionchange map&#39;,
<a name="line118"></a>      plugin,
<a name="line119"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.SELECTION][0]);
<a name="line120"></a>  assertEquals(&#39;Plugin has a shortcut handler, should be in shortcut map&#39;,
<a name="line121"></a>      plugin,
<a name="line122"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.SHORTCUT][0]);
<a name="line123"></a>  assertEquals(&#39;Plugin has a execCommand, should be in execCommand map&#39;,
<a name="line124"></a>      plugin,
<a name="line125"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.EXEC_COMMAND][0]);
<a name="line126"></a>  assertEquals(&#39;Plugin has a queryCommand, should be in queryCommand map&#39;,
<a name="line127"></a>      plugin,
<a name="line128"></a>      editableField.indexedPlugins_[goog.editor.Plugin.Op.QUERY_COMMAND][0]);
<a name="line129"></a>  assertEquals(&#39;Plugin does not have a prepareContentsHtml,&#39; +
<a name="line130"></a>      &#39;should not be in prepareContentsHtml map&#39;,
<a name="line131"></a>      undefined,
<a name="line132"></a>      editableField.indexedPlugins_[
<a name="line133"></a>          goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML][0]);
<a name="line134"></a>  assertEquals(&#39;Plugin does not have a cleanContentsDom,&#39; +
<a name="line135"></a>      &#39;should not be in cleanContentsDom map&#39;,
<a name="line136"></a>      undefined,
<a name="line137"></a>      editableField.indexedPlugins_[
<a name="line138"></a>          goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM][0]);
<a name="line139"></a>  assertEquals(&#39;Plugin does not have a cleanContentsHtml,&#39; +
<a name="line140"></a>      &#39;should not be in cleanContentsHtml map&#39;,
<a name="line141"></a>      undefined,
<a name="line142"></a>      editableField.indexedPlugins_[
<a name="line143"></a>          goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML][0]);
<a name="line144"></a>
<a name="line145"></a>  editableField.dispose();
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Tests that calling unregisterPlugin will remove the plugin from
<a name="line151"></a> * the map.
<a name="line152"></a> */
<a name="line153"></a>function testUnregisterPlugin() {
<a name="line154"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line155"></a>  var plugin = new TestPlugin();
<a name="line156"></a>
<a name="line157"></a>  editableField.registerPlugin(plugin);
<a name="line158"></a>  editableField.unregisterPlugin(plugin);
<a name="line159"></a>
<a name="line160"></a>  assertUndefined(&#39;Unregistered plugin must not be in protected plugin map.&#39;,
<a name="line161"></a>      editableField.plugins_[plugin.getTrogClassId()]);
<a name="line162"></a>
<a name="line163"></a>  editableField.dispose();
<a name="line164"></a>}
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Tests that registered plugins can be fetched by their id.
<a name="line169"></a> */
<a name="line170"></a>function testGetPluginByClassId() {
<a name="line171"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line172"></a>  var plugin = new TestPlugin();
<a name="line173"></a>
<a name="line174"></a>  assertUndefined(&#39;Must not be able to get unregistered plugins by class id.&#39;,
<a name="line175"></a>      editableField.getPluginByClassId(plugin.getTrogClassId()));
<a name="line176"></a>
<a name="line177"></a>  editableField.registerPlugin(plugin);
<a name="line178"></a>  assertEquals(&#39;Must be able to get registered plugins by class id.&#39;,
<a name="line179"></a>      plugin, editableField.getPluginByClassId(plugin.getTrogClassId()));
<a name="line180"></a>  editableField.dispose();
<a name="line181"></a>}
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Tests that plugins get auto disposed by default when the field is disposed.
<a name="line186"></a> * Tests that plugins with setAutoDispose(false) do not get disposed when the
<a name="line187"></a> * field is disposed.
<a name="line188"></a> */
<a name="line189"></a>function testDisposed_PluginAutoDispose() {
<a name="line190"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line191"></a>  var plugin = new TestPlugin();
<a name="line192"></a>
<a name="line193"></a>  var noDisposePlugin = new goog.editor.Plugin();
<a name="line194"></a>  noDisposePlugin.getTrogClassId = function() {
<a name="line195"></a>    return &#39;noDisposeId&#39;;
<a name="line196"></a>  };
<a name="line197"></a>  noDisposePlugin.setAutoDispose(false);
<a name="line198"></a>
<a name="line199"></a>  editableField.registerPlugin(plugin);
<a name="line200"></a>  editableField.registerPlugin(noDisposePlugin);
<a name="line201"></a>  editableField.dispose();
<a name="line202"></a>  assert(editableField.isDisposed());
<a name="line203"></a>  assertTrue(plugin.isDisposed());
<a name="line204"></a>  assertFalse(noDisposePlugin.isDisposed());
<a name="line205"></a>}
<a name="line206"></a>
<a name="line207"></a>var STRING_KEY = String.fromCharCode(goog.events.KeyCodes.A).toLowerCase();
<a name="line208"></a>
<a name="line209"></a>/**
<a name="line210"></a> * @return {goog.events.Event} Returns an event for a keyboard shortcut
<a name="line211"></a> * for the letter &#39;a&#39;
<a name="line212"></a> */
<a name="line213"></a>function getBrowserEvent() {
<a name="line214"></a>  var e = new goog.events.BrowserEvent();
<a name="line215"></a>  e.ctrlKey = true;
<a name="line216"></a>  e.metaKey = true;
<a name="line217"></a>  e.charCode = goog.events.KeyCodes.A;
<a name="line218"></a>  return e;
<a name="line219"></a>}
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Tests that plugins are disabled when the field is made uneditable.
<a name="line224"></a> */
<a name="line225"></a>
<a name="line226"></a>function testMakeUneditableDisablesPlugins() {
<a name="line227"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line228"></a>  var plugin = new TestPlugin();
<a name="line229"></a>
<a name="line230"></a>  var calls = 0;
<a name="line231"></a>  plugin.disable = function(field) {
<a name="line232"></a>    assertEquals(editableField, field);
<a name="line233"></a>    assertTrue(field.isUneditable());
<a name="line234"></a>    calls++;
<a name="line235"></a>  };
<a name="line236"></a>
<a name="line237"></a>  editableField.registerPlugin(plugin);
<a name="line238"></a>  editableField.makeEditable();
<a name="line239"></a>
<a name="line240"></a>  assertEquals(0, calls);
<a name="line241"></a>  editableField.makeUneditable();
<a name="line242"></a>
<a name="line243"></a>  assertEquals(1, calls);
<a name="line244"></a>
<a name="line245"></a>  editableField.dispose();
<a name="line246"></a>}
<a name="line247"></a>
<a name="line248"></a>
<a name="line249"></a>/**
<a name="line250"></a> * Test that if a plugin registers keyup, it gets called.
<a name="line251"></a> */
<a name="line252"></a>function testPluginKeyUp() {
<a name="line253"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line254"></a>  var plugin = new TestPlugin();
<a name="line255"></a>  var e = getBrowserEvent();
<a name="line256"></a>
<a name="line257"></a>  var mockPlugin = new goog.testing.LooseMock(plugin);
<a name="line258"></a>  mockPlugin.getTrogClassId().$returns(&#39;mockPlugin&#39;);
<a name="line259"></a>  mockPlugin.registerFieldObject(editableField);
<a name="line260"></a>  mockPlugin.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line261"></a>  mockPlugin.handleKeyUp(e);
<a name="line262"></a>  mockPlugin.$replay();
<a name="line263"></a>
<a name="line264"></a>  editableField.registerPlugin(mockPlugin);
<a name="line265"></a>
<a name="line266"></a>  editableField.handleKeyUp_(e);
<a name="line267"></a>
<a name="line268"></a>  mockPlugin.$verify();
<a name="line269"></a>}
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>/**
<a name="line273"></a> * Test that if a plugin registers keydown, it gets called.
<a name="line274"></a> */
<a name="line275"></a>function testPluginKeyDown() {
<a name="line276"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line277"></a>  var plugin = new TestPlugin();
<a name="line278"></a>  var e = getBrowserEvent();
<a name="line279"></a>
<a name="line280"></a>  var mockPlugin = new goog.testing.LooseMock(plugin);
<a name="line281"></a>  mockPlugin.getTrogClassId().$returns(&#39;mockPlugin&#39;);
<a name="line282"></a>  mockPlugin.registerFieldObject(editableField);
<a name="line283"></a>  mockPlugin.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line284"></a>  mockPlugin.handleKeyDown(e).$returns(true);
<a name="line285"></a>  mockPlugin.$replay();
<a name="line286"></a>
<a name="line287"></a>  editableField.registerPlugin(mockPlugin);
<a name="line288"></a>
<a name="line289"></a>  editableField.handleKeyDown_(e);
<a name="line290"></a>
<a name="line291"></a>  mockPlugin.$verify();
<a name="line292"></a>}
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Test that if a plugin registers keypress, it gets called.
<a name="line297"></a> */
<a name="line298"></a>function testPluginKeyPress() {
<a name="line299"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line300"></a>  var plugin = new TestPlugin();
<a name="line301"></a>  var e = getBrowserEvent();
<a name="line302"></a>
<a name="line303"></a>  var mockPlugin = new goog.testing.LooseMock(plugin);
<a name="line304"></a>  mockPlugin.getTrogClassId().$returns(&#39;mockPlugin&#39;);
<a name="line305"></a>  mockPlugin.registerFieldObject(editableField);
<a name="line306"></a>  mockPlugin.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line307"></a>  mockPlugin.handleKeyPress(e).$returns(true);
<a name="line308"></a>  mockPlugin.$replay();
<a name="line309"></a>
<a name="line310"></a>  editableField.registerPlugin(mockPlugin);
<a name="line311"></a>
<a name="line312"></a>  editableField.handleKeyPress_(e);
<a name="line313"></a>
<a name="line314"></a>  mockPlugin.$verify();
<a name="line315"></a>}
<a name="line316"></a>
<a name="line317"></a>
<a name="line318"></a>/**
<a name="line319"></a> * If one plugin handles a key event, the rest of the plugins do not get their
<a name="line320"></a> * key handlers invoked.
<a name="line321"></a> */
<a name="line322"></a>function testHandledKeyEvent() {
<a name="line323"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line324"></a>  var plugin = new TestPlugin();
<a name="line325"></a>  var e = getBrowserEvent();
<a name="line326"></a>
<a name="line327"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line328"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line329"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line330"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line331"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line332"></a>    mockPlugin1.handleKeyDown(e).$returns(true);
<a name="line333"></a>  } else {
<a name="line334"></a>    mockPlugin1.handleKeyPress(e).$returns(true);
<a name="line335"></a>  }
<a name="line336"></a>  mockPlugin1.handleKeyUp(e).$returns(true);
<a name="line337"></a>  mockPlugin1.$replay();
<a name="line338"></a>
<a name="line339"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line340"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line341"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line342"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line343"></a>  mockPlugin2.$replay();
<a name="line344"></a>
<a name="line345"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line346"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line347"></a>
<a name="line348"></a>  editableField.handleKeyUp_(e);
<a name="line349"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line350"></a>    editableField.handleKeyDown_(e);
<a name="line351"></a>  } else {
<a name="line352"></a>    editableField.handleKeyPress_(e);
<a name="line353"></a>  }
<a name="line354"></a>
<a name="line355"></a>  mockPlugin1.$verify();
<a name="line356"></a>  mockPlugin2.$verify();
<a name="line357"></a>}
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * Tests to make sure the cut and paste events are not dispatched immediately.
<a name="line362"></a> */
<a name="line363"></a>function testHandleCutAndPasteEvents() {
<a name="line364"></a>  if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
<a name="line365"></a>    // Cut and paste events do not raise events at all in Mozilla.
<a name="line366"></a>    return;
<a name="line367"></a>  }
<a name="line368"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line369"></a>  var clock = new goog.testing.MockClock(true);
<a name="line370"></a>  var delayedChanges = goog.testing.recordFunction();
<a name="line371"></a>  goog.events.listen(editableField, goog.editor.Field.EventType.DELAYEDCHANGE,
<a name="line372"></a>      delayedChanges);
<a name="line373"></a>
<a name="line374"></a>  editableField.makeEditable();
<a name="line375"></a>
<a name="line376"></a>  goog.testing.events.fireBrowserEvent(
<a name="line377"></a>      new goog.testing.events.Event(&#39;cut&#39;, editableField.getElement()));
<a name="line378"></a>  assertEquals(&#39;Cut event should be on a timer&#39;, 0,
<a name="line379"></a>      delayedChanges.getCallCount());
<a name="line380"></a>  clock.tick(1000);
<a name="line381"></a>  assertEquals(&#39;delayed change event should fire within 1s after cut&#39;, 1,
<a name="line382"></a>      delayedChanges.getCallCount());
<a name="line383"></a>
<a name="line384"></a>  goog.testing.events.fireBrowserEvent(
<a name="line385"></a>      new goog.testing.events.Event(&#39;paste&#39;, editableField.getElement()));
<a name="line386"></a>  assertEquals(&#39;Paste event should be on a timer&#39;, 1,
<a name="line387"></a>      delayedChanges.getCallCount());
<a name="line388"></a>  clock.tick(1000);
<a name="line389"></a>  assertEquals(&#39;delayed change event should fire within 1s after paste&#39;, 2,
<a name="line390"></a>      delayedChanges.getCallCount());
<a name="line391"></a>
<a name="line392"></a>  clock.dispose();
<a name="line393"></a>  editableField.dispose();
<a name="line394"></a>}
<a name="line395"></a>
<a name="line396"></a>
<a name="line397"></a>/**
<a name="line398"></a> * If the first plugin does not handle the key event, the next plugin gets
<a name="line399"></a> * a chance to handle it.
<a name="line400"></a> */
<a name="line401"></a>function testNotHandledKeyEvent() {
<a name="line402"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line403"></a>  var plugin = new TestPlugin();
<a name="line404"></a>  var e = getBrowserEvent();
<a name="line405"></a>
<a name="line406"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line407"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line408"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line409"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line410"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line411"></a>    mockPlugin1.handleKeyDown(e).$returns(false);
<a name="line412"></a>  } else {
<a name="line413"></a>    mockPlugin1.handleKeyPress(e).$returns(false);
<a name="line414"></a>  }
<a name="line415"></a>  mockPlugin1.handleKeyUp(e).$returns(false);
<a name="line416"></a>  mockPlugin1.$replay();
<a name="line417"></a>
<a name="line418"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line419"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line420"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line421"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line422"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line423"></a>    mockPlugin2.handleKeyDown(e).$returns(true);
<a name="line424"></a>  } else {
<a name="line425"></a>    mockPlugin2.handleKeyPress(e).$returns(true);
<a name="line426"></a>  }
<a name="line427"></a>  mockPlugin2.handleKeyUp(e).$returns(true);
<a name="line428"></a>  mockPlugin2.$replay();
<a name="line429"></a>
<a name="line430"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line431"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line432"></a>
<a name="line433"></a>  editableField.handleKeyUp_(e);
<a name="line434"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line435"></a>    editableField.handleKeyDown_(e);
<a name="line436"></a>  } else {
<a name="line437"></a>    editableField.handleKeyPress_(e);
<a name="line438"></a>  }
<a name="line439"></a>
<a name="line440"></a>  mockPlugin1.$verify();
<a name="line441"></a>  mockPlugin2.$verify();
<a name="line442"></a>}
<a name="line443"></a>
<a name="line444"></a>
<a name="line445"></a>/**
<a name="line446"></a> * Make sure that handleKeyboardShortcut is called if other key handlers
<a name="line447"></a> * return false.
<a name="line448"></a> */
<a name="line449"></a>function testKeyboardShortcutCalled() {
<a name="line450"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line451"></a>  var plugin = new TestPlugin();
<a name="line452"></a>  var e = getBrowserEvent();
<a name="line453"></a>
<a name="line454"></a>  var mockPlugin = new goog.testing.LooseMock(plugin);
<a name="line455"></a>  mockPlugin.getTrogClassId().$returns(&#39;mockPlugin&#39;);
<a name="line456"></a>  mockPlugin.registerFieldObject(editableField);
<a name="line457"></a>  mockPlugin.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line458"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line459"></a>    mockPlugin.handleKeyDown(e).$returns(false);
<a name="line460"></a>  } else {
<a name="line461"></a>    mockPlugin.handleKeyPress(e).$returns(false);
<a name="line462"></a>  }
<a name="line463"></a>  mockPlugin.handleKeyboardShortcut(e, STRING_KEY, true).$returns(false);
<a name="line464"></a>  mockPlugin.$replay();
<a name="line465"></a>
<a name="line466"></a>  editableField.registerPlugin(mockPlugin);
<a name="line467"></a>
<a name="line468"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line469"></a>    editableField.handleKeyDown_(e);
<a name="line470"></a>  } else {
<a name="line471"></a>    editableField.handleKeyPress_(e);
<a name="line472"></a>  }
<a name="line473"></a>
<a name="line474"></a>  mockPlugin.$verify();
<a name="line475"></a>}
<a name="line476"></a>
<a name="line477"></a>
<a name="line478"></a>/**
<a name="line479"></a> * Make sure that handleKeyboardShortcut is not called if other key handlers
<a name="line480"></a> * return true.
<a name="line481"></a> */
<a name="line482"></a>function testKeyboardShortcutNotCalled() {
<a name="line483"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line484"></a>  var plugin = new TestPlugin();
<a name="line485"></a>  var e = getBrowserEvent();
<a name="line486"></a>
<a name="line487"></a>  var mockPlugin = new goog.testing.LooseMock(plugin);
<a name="line488"></a>  mockPlugin.getTrogClassId().$returns(&#39;mockPlugin&#39;);
<a name="line489"></a>  mockPlugin.registerFieldObject(editableField);
<a name="line490"></a>  mockPlugin.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line491"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line492"></a>    mockPlugin.handleKeyDown(e).$returns(true);
<a name="line493"></a>  } else {
<a name="line494"></a>    mockPlugin.handleKeyPress(e).$returns(true);
<a name="line495"></a>  }
<a name="line496"></a>  mockPlugin.$replay();
<a name="line497"></a>
<a name="line498"></a>  editableField.registerPlugin(mockPlugin);
<a name="line499"></a>
<a name="line500"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line501"></a>    editableField.handleKeyDown_(e);
<a name="line502"></a>  } else {
<a name="line503"></a>    editableField.handleKeyPress_(e);
<a name="line504"></a>  }
<a name="line505"></a>
<a name="line506"></a>  mockPlugin.$verify();
<a name="line507"></a>}
<a name="line508"></a>
<a name="line509"></a>
<a name="line510"></a>/**
<a name="line511"></a> * Make sure that handleKeyboardShortcut is not called if alt is pressed.
<a name="line512"></a> * @bug 1363959
<a name="line513"></a> */
<a name="line514"></a>function testKeyHandlingAlt() {
<a name="line515"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line516"></a>  var plugin = new TestPlugin();
<a name="line517"></a>  var e = getBrowserEvent();
<a name="line518"></a>  e.altKey = true;
<a name="line519"></a>
<a name="line520"></a>  var mockPlugin = new goog.testing.LooseMock(plugin);
<a name="line521"></a>  mockPlugin.getTrogClassId().$returns(&#39;mockPlugin&#39;);
<a name="line522"></a>  mockPlugin.registerFieldObject(editableField);
<a name="line523"></a>  mockPlugin.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line524"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line525"></a>    mockPlugin.handleKeyDown(e).$returns(false);
<a name="line526"></a>  } else {
<a name="line527"></a>    mockPlugin.handleKeyPress(e).$returns(false);
<a name="line528"></a>  }
<a name="line529"></a>  mockPlugin.$replay();
<a name="line530"></a>
<a name="line531"></a>  editableField.registerPlugin(mockPlugin);
<a name="line532"></a>
<a name="line533"></a>  if (goog.editor.BrowserFeature.USES_KEYDOWN) {
<a name="line534"></a>    editableField.handleKeyDown_(e);
<a name="line535"></a>  } else {
<a name="line536"></a>    editableField.handleKeyPress_(e);
<a name="line537"></a>  }
<a name="line538"></a>
<a name="line539"></a>  mockPlugin.$verify();
<a name="line540"></a>}
<a name="line541"></a>
<a name="line542"></a>
<a name="line543"></a>/**
<a name="line544"></a> * Test that if a plugin has an execCommand function, it gets called
<a name="line545"></a> * but only for supported commands.
<a name="line546"></a> */
<a name="line547"></a>function testPluginExecCommand() {
<a name="line548"></a>  var plugin = new TestPlugin();
<a name="line549"></a>  var passedCommand, passedArg;
<a name="line550"></a>  plugin.execCommand = function(command, arg) {
<a name="line551"></a>    passedCommand = command;
<a name="line552"></a>    passedArg = arg;
<a name="line553"></a>  }
<a name="line554"></a>
<a name="line555"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line556"></a>  editableField.registerPlugin(plugin);
<a name="line557"></a>  plugin.enable(editableField);
<a name="line558"></a>  plugin.isSupportedCommand = goog.functions.constant(true);
<a name="line559"></a>
<a name="line560"></a>  editableField.execCommand(&#39;+indent&#39;, true);
<a name="line561"></a>  // Verify that the plugin&#39;s execCommand was called with the correct
<a name="line562"></a>  // args.
<a name="line563"></a>  assertEquals(&#39;+indent&#39;, passedCommand);
<a name="line564"></a>  assertTrue(passedArg);
<a name="line565"></a>
<a name="line566"></a>  passedCommand = null;
<a name="line567"></a>  passedArg = null;
<a name="line568"></a>  plugin.isSupportedCommand = goog.functions.constant(false);
<a name="line569"></a>
<a name="line570"></a>  editableField.execCommand(&#39;+outdent&#39;, false);
<a name="line571"></a>  // Verify that a plugin&#39;s execCommand is not called if it isn&#39;t a supported
<a name="line572"></a>  // command.
<a name="line573"></a>  assertNull(passedCommand);
<a name="line574"></a>  assertNull(passedArg);
<a name="line575"></a>
<a name="line576"></a>  editableField.dispose();
<a name="line577"></a>  plugin.dispose();
<a name="line578"></a>}
<a name="line579"></a>
<a name="line580"></a>
<a name="line581"></a>/**
<a name="line582"></a> * Test that if one plugin supports execCommand, no other plugins
<a name="line583"></a> * get a chance to handle the execComand.
<a name="line584"></a> */
<a name="line585"></a>function testSupportedExecCommand() {
<a name="line586"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line587"></a>  var plugin = new TestPlugin();
<a name="line588"></a>
<a name="line589"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line590"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line591"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line592"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line593"></a>  mockPlugin1.isSupportedCommand(&#39;+indent&#39;).$returns(true);
<a name="line594"></a>  mockPlugin1.execCommandInternal(&#39;+indent&#39;).$returns(true);
<a name="line595"></a>  mockPlugin1.execCommand(&#39;+indent&#39;).$does(
<a name="line596"></a>      function() { mockPlugin1.execCommandInternal(&#39;+indent&#39;); });
<a name="line597"></a>  mockPlugin1.$replay();
<a name="line598"></a>
<a name="line599"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line600"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line601"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line602"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line603"></a>  mockPlugin2.$replay();
<a name="line604"></a>
<a name="line605"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line606"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line607"></a>
<a name="line608"></a>  editableField.execCommand(&#39;+indent&#39;);
<a name="line609"></a>
<a name="line610"></a>  mockPlugin1.$verify();
<a name="line611"></a>  mockPlugin2.$verify();
<a name="line612"></a>}
<a name="line613"></a>
<a name="line614"></a>
<a name="line615"></a>/**
<a name="line616"></a> * Test that if the first plugin does not support execCommand, the other
<a name="line617"></a> * plugins get a chance to handle the execCommand.
<a name="line618"></a> */
<a name="line619"></a>function testNotSupportedExecCommand() {
<a name="line620"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line621"></a>  var plugin = new TestPlugin();
<a name="line622"></a>
<a name="line623"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line624"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line625"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line626"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line627"></a>  mockPlugin1.isSupportedCommand(&#39;+indent&#39;).$returns(false);
<a name="line628"></a>  mockPlugin1.$replay();
<a name="line629"></a>
<a name="line630"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line631"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line632"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line633"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line634"></a>  mockPlugin2.isSupportedCommand(&#39;+indent&#39;).$returns(true);
<a name="line635"></a>  mockPlugin2.execCommandInternal(&#39;+indent&#39;).$returns(true);
<a name="line636"></a>  mockPlugin2.execCommand(&#39;+indent&#39;).$does(
<a name="line637"></a>      function() { mockPlugin2.execCommandInternal(&#39;+indent&#39;); });
<a name="line638"></a>  mockPlugin2.$replay();
<a name="line639"></a>
<a name="line640"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line641"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line642"></a>
<a name="line643"></a>  editableField.execCommand(&#39;+indent&#39;);
<a name="line644"></a>
<a name="line645"></a>  mockPlugin1.$verify();
<a name="line646"></a>  mockPlugin2.$verify();
<a name="line647"></a>}
<a name="line648"></a>
<a name="line649"></a>
<a name="line650"></a>/**
<a name="line651"></a> * Tests that if a plugin supports a command that its queryCommandValue
<a name="line652"></a> * gets called and no further plugins can handle the queryCommandValue.
<a name="line653"></a> */
<a name="line654"></a>function testSupportedQueryCommand() {
<a name="line655"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line656"></a>  var plugin = new TestPlugin();
<a name="line657"></a>
<a name="line658"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line659"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line660"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line661"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line662"></a>  mockPlugin1.isSupportedCommand(&#39;+indent&#39;).$returns(true);
<a name="line663"></a>  mockPlugin1.queryCommandValue(&#39;+indent&#39;).$returns(true);
<a name="line664"></a>  mockPlugin1.activeOnUneditableFields().$returns(true);
<a name="line665"></a>  mockPlugin1.$replay();
<a name="line666"></a>
<a name="line667"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line668"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line669"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line670"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line671"></a>  mockPlugin2.$replay();
<a name="line672"></a>
<a name="line673"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line674"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line675"></a>
<a name="line676"></a>  editableField.queryCommandValue(&#39;+indent&#39;);
<a name="line677"></a>
<a name="line678"></a>  mockPlugin1.$verify();
<a name="line679"></a>  mockPlugin2.$verify();
<a name="line680"></a>}
<a name="line681"></a>
<a name="line682"></a>
<a name="line683"></a>/**
<a name="line684"></a> * Tests that if the first plugin does not support a command that its
<a name="line685"></a> * queryCommandValue do not get called and the next plugin can handle the
<a name="line686"></a> * queryCommandValue.
<a name="line687"></a> */
<a name="line688"></a>function testNotSupportedQueryCommand() {
<a name="line689"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line690"></a>  var plugin = new TestPlugin();
<a name="line691"></a>
<a name="line692"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line693"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line694"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line695"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line696"></a>  mockPlugin1.isSupportedCommand(&#39;+indent&#39;).$returns(false);
<a name="line697"></a>  mockPlugin1.$replay();
<a name="line698"></a>
<a name="line699"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line700"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line701"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line702"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line703"></a>  mockPlugin2.isSupportedCommand(&#39;+indent&#39;).$returns(true);
<a name="line704"></a>  mockPlugin2.queryCommandValue(&#39;+indent&#39;).$returns(true);
<a name="line705"></a>  mockPlugin2.activeOnUneditableFields().$returns(true);
<a name="line706"></a>  mockPlugin2.$replay();
<a name="line707"></a>
<a name="line708"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line709"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line710"></a>
<a name="line711"></a>  editableField.queryCommandValue(&#39;+indent&#39;);
<a name="line712"></a>
<a name="line713"></a>  mockPlugin1.$verify();
<a name="line714"></a>  mockPlugin2.$verify();
<a name="line715"></a>}
<a name="line716"></a>
<a name="line717"></a>
<a name="line718"></a>/**
<a name="line719"></a> * Tests that if a plugin handles selectionChange that it gets called and
<a name="line720"></a> * no further plugins can handle the selectionChange.
<a name="line721"></a> */
<a name="line722"></a>function testHandledSelectionChange() {
<a name="line723"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line724"></a>  var plugin = new TestPlugin();
<a name="line725"></a>  var e = getBrowserEvent();
<a name="line726"></a>
<a name="line727"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line728"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line729"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line730"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line731"></a>  mockPlugin1.handleSelectionChange(e, undefined).$returns(true);
<a name="line732"></a>  mockPlugin1.$replay();
<a name="line733"></a>
<a name="line734"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line735"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line736"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line737"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line738"></a>  mockPlugin2.$replay();
<a name="line739"></a>
<a name="line740"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line741"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line742"></a>
<a name="line743"></a>  editableField.dispatchSelectionChangeEvent(e);
<a name="line744"></a>
<a name="line745"></a>  mockPlugin1.$verify();
<a name="line746"></a>  mockPlugin2.$verify();
<a name="line747"></a>}
<a name="line748"></a>
<a name="line749"></a>
<a name="line750"></a>/**
<a name="line751"></a> * Tests that if the first plugin does not handle selectionChange that
<a name="line752"></a> * the next plugin gets a chance to handle it.
<a name="line753"></a> */
<a name="line754"></a>function testNotHandledSelectionChange() {
<a name="line755"></a>  var editableField = new FieldConstructor(&#39;testField&#39;);
<a name="line756"></a>  var plugin = new TestPlugin();
<a name="line757"></a>  var e = getBrowserEvent();
<a name="line758"></a>
<a name="line759"></a>  var mockPlugin1 = new goog.testing.LooseMock(plugin);
<a name="line760"></a>  mockPlugin1.getTrogClassId().$returns(&#39;mockPlugin1&#39;);
<a name="line761"></a>  mockPlugin1.registerFieldObject(editableField);
<a name="line762"></a>  mockPlugin1.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line763"></a>  mockPlugin1.handleSelectionChange(e, undefined).$returns(false);
<a name="line764"></a>  mockPlugin1.$replay();
<a name="line765"></a>
<a name="line766"></a>  var mockPlugin2 = new goog.testing.LooseMock(plugin);
<a name="line767"></a>  mockPlugin2.getTrogClassId().$returns(&#39;mockPlugin2&#39;);
<a name="line768"></a>  mockPlugin2.registerFieldObject(editableField);
<a name="line769"></a>  mockPlugin2.isEnabled(editableField).$anyTimes().$returns(true);
<a name="line770"></a>  mockPlugin2.handleSelectionChange(e, undefined).$returns(true);
<a name="line771"></a>  mockPlugin2.$replay();
<a name="line772"></a>
<a name="line773"></a>  editableField.registerPlugin(mockPlugin1);
<a name="line774"></a>  editableField.registerPlugin(mockPlugin2);
<a name="line775"></a>
<a name="line776"></a>  editableField.dispatchSelectionChangeEvent(e);
<a name="line777"></a>
<a name="line778"></a>  mockPlugin1.$verify();
<a name="line779"></a>  mockPlugin2.$verify();
<a name="line780"></a>}
<a name="line781"></a>
<a name="line782"></a>
<a name="line783"></a>// Tests for goog.editor.Field internals.
<a name="line784"></a>
<a name="line785"></a>function testSelectionChange() {
<a name="line786"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line787"></a>  var clock = new goog.testing.MockClock(true);
<a name="line788"></a>  var selectionChanges = goog.testing.recordFunction();
<a name="line789"></a>  goog.events.listen(editableField, goog.editor.Field.EventType.SELECTIONCHANGE,
<a name="line790"></a>      selectionChanges);
<a name="line791"></a>
<a name="line792"></a>  editableField.makeEditable();
<a name="line793"></a>
<a name="line794"></a>  // Emulate pressing left arrow key, this should result in SELECTIONCHANGE
<a name="line795"></a>  // event after a short timeout.
<a name="line796"></a>  editableField.handleKeyUp_({keyCode: goog.events.KeyCodes.LEFT});
<a name="line797"></a>  assertEquals(&#39;Selection change should be on a timer&#39;, 0,
<a name="line798"></a>      selectionChanges.getCallCount());
<a name="line799"></a>  clock.tick(1000);
<a name="line800"></a>  assertEquals(&#39;Selection change should fire within 1s&#39;, 1,
<a name="line801"></a>      selectionChanges.getCallCount());
<a name="line802"></a>
<a name="line803"></a>  // Programically place cursor at start. SELECTIONCHANGE event should be fired.
<a name="line804"></a>  editableField.placeCursorAtStart();
<a name="line805"></a>  assertEquals(&#39;Selection change should fire&#39;, 2,
<a name="line806"></a>      selectionChanges.getCallCount());
<a name="line807"></a>
<a name="line808"></a>  clock.dispose();
<a name="line809"></a>  editableField.dispose();
<a name="line810"></a>}
<a name="line811"></a>
<a name="line812"></a>
<a name="line813"></a>function testSelectionChangeOnMouseUp() {
<a name="line814"></a>  var fakeEvent =
<a name="line815"></a>      new goog.events.BrowserEvent({type: &#39;mouseup&#39;, target: &#39;fakeTarget&#39;});
<a name="line816"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line817"></a>  var clock = new goog.testing.MockClock(true);
<a name="line818"></a>  var selectionChanges = goog.testing.recordFunction();
<a name="line819"></a>  goog.events.listen(editableField, goog.editor.Field.EventType.SELECTIONCHANGE,
<a name="line820"></a>      selectionChanges);
<a name="line821"></a>
<a name="line822"></a>  var plugin = new TestPlugin();
<a name="line823"></a>  plugin.handleSelectionChange = goog.testing.recordFunction();
<a name="line824"></a>  editableField.registerPlugin(plugin);
<a name="line825"></a>
<a name="line826"></a>  editableField.makeEditable();
<a name="line827"></a>
<a name="line828"></a>  // Emulate a mouseup event, this should result in an immediate
<a name="line829"></a>  // SELECTIONCHANGE, plus a second one in IE after a short timeout.
<a name="line830"></a>  editableField.handleMouseUp_(fakeEvent);
<a name="line831"></a>  assertEquals(&#39;Selection change should fire immediately&#39;, 1,
<a name="line832"></a>      selectionChanges.getCallCount());
<a name="line833"></a>  assertEquals(&#39;Plugin should have handled selection change immediately&#39;, 1,
<a name="line834"></a>      plugin.handleSelectionChange.getCallCount());
<a name="line835"></a>  assertEquals(&#39;Plugin should have received original browser event to handle&#39;,
<a name="line836"></a>      fakeEvent, plugin.handleSelectionChange.getLastCall().getArguments()[0]);
<a name="line837"></a>
<a name="line838"></a>  // Pretend another plugin fired a SELECTIONCHANGE in the meantime.
<a name="line839"></a>  editableField.dispatchSelectionChangeEvent();
<a name="line840"></a>  assertEquals(&#39;Second selection change should fire immediately&#39;, 2,
<a name="line841"></a>      selectionChanges.getCallCount());
<a name="line842"></a>  assertEquals(&#39;Plugin should have handled second selection change immediately&#39;,
<a name="line843"></a>       2,  plugin.handleSelectionChange.getCallCount());
<a name="line844"></a>  var args = plugin.handleSelectionChange.getLastCall().getArguments();
<a name="line845"></a>  assertTrue(&#39;Plugin should not have received data from extra firing&#39;,
<a name="line846"></a>      args.length == 0 ||
<a name="line847"></a>      !args[0] &amp;&amp; (args.length == 1 || !args[1]));
<a name="line848"></a>
<a name="line849"></a>  // Now check for the extra call in IE.
<a name="line850"></a>  clock.tick(1000);
<a name="line851"></a>  if (goog.userAgent.IE) {
<a name="line852"></a>    assertEquals(&#39;Additional selection change should fire within 1s&#39;, 3,
<a name="line853"></a>        selectionChanges.getCallCount());
<a name="line854"></a>    assertEquals(&#39;Plugin should have handled selection change within 1s&#39;, 3,
<a name="line855"></a>        plugin.handleSelectionChange.getCallCount());
<a name="line856"></a>    assertEquals(&#39;Plugin should have received target of original browser event&#39;,
<a name="line857"></a>        fakeEvent.target,
<a name="line858"></a>        plugin.handleSelectionChange.getLastCall().getArguments().pop());
<a name="line859"></a>  } else {
<a name="line860"></a>    assertEquals(&#39;No additional selection change should fire&#39;, 2,
<a name="line861"></a>        selectionChanges.getCallCount());
<a name="line862"></a>    assertEquals(&#39;Plugin should not have handled selection change again&#39;, 2,
<a name="line863"></a>        plugin.handleSelectionChange.getCallCount());
<a name="line864"></a>  }
<a name="line865"></a>
<a name="line866"></a>  clock.dispose();
<a name="line867"></a>  editableField.dispose();
<a name="line868"></a>}
<a name="line869"></a>
<a name="line870"></a>
<a name="line871"></a>function testSelectionChangeBeforeUneditable() {
<a name="line872"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line873"></a>  var clock = new goog.testing.MockClock(true);
<a name="line874"></a>  var selectionChanges = goog.testing.recordFunction();
<a name="line875"></a>  goog.events.listen(editableField, goog.editor.Field.EventType.SELECTIONCHANGE,
<a name="line876"></a>      selectionChanges);
<a name="line877"></a>
<a name="line878"></a>  editableField.makeEditable();
<a name="line879"></a>  editableField.handleKeyUp_({keyCode: goog.events.KeyCodes.LEFT});
<a name="line880"></a>  assertEquals(&#39;Selection change should be on a timer&#39;, 0,
<a name="line881"></a>      selectionChanges.getCallCount());
<a name="line882"></a>  editableField.makeUneditable();
<a name="line883"></a>  assertEquals(&#39;Selection change should fire during make uneditable&#39;, 1,
<a name="line884"></a>      selectionChanges.getCallCount());
<a name="line885"></a>  clock.tick(1000);
<a name="line886"></a>  assertEquals(&#39;No additional selection change should fire&#39;, 1,
<a name="line887"></a>      selectionChanges.getCallCount());
<a name="line888"></a>
<a name="line889"></a>  clock.dispose();
<a name="line890"></a>  editableField.dispose();
<a name="line891"></a>}
<a name="line892"></a>
<a name="line893"></a>
<a name="line894"></a>function testGetEditableDomHelper() {
<a name="line895"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line896"></a>  assertNull(&#39;Before being made editable, we do not know the dom helper&#39;,
<a name="line897"></a>      editableField.getEditableDomHelper());
<a name="line898"></a>  editableField.makeEditable();
<a name="line899"></a>  assertNotNull(&#39;After being made editable, we know the dom helper&#39;,
<a name="line900"></a>      editableField.getEditableDomHelper());
<a name="line901"></a>  assertEquals(&#39;Document from domHelper should be the editable elements doc&#39;,
<a name="line902"></a>      goog.dom.getOwnerDocument(editableField.getElement()),
<a name="line903"></a>      editableField.getEditableDomHelper().getDocument());
<a name="line904"></a>  editableField.dispose();
<a name="line905"></a>}
<a name="line906"></a>
<a name="line907"></a>
<a name="line908"></a>function testQueryCommandValue() {
<a name="line909"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line910"></a>  assertFalse(editableField.queryCommandValue(&#39;boo&#39;));
<a name="line911"></a>  assertObjectEquals({&#39;boo&#39;: false, &#39;aieee&#39;: false},
<a name="line912"></a>      editableField.queryCommandValue([&#39;boo&#39;, &#39;aieee&#39;]));
<a name="line913"></a>
<a name="line914"></a>  editableField.makeEditable();
<a name="line915"></a>  assertFalse(editableField.queryCommandValue(&#39;boo&#39;));
<a name="line916"></a>
<a name="line917"></a>  editableField.getElement().focus();
<a name="line918"></a>  editableField.dispatchSelectionChangeEvent();
<a name="line919"></a>  assertNull(editableField.queryCommandValue(&#39;boo&#39;));
<a name="line920"></a>  assertObjectEquals({&#39;boo&#39;: null, &#39;aieee&#39;: null},
<a name="line921"></a>      editableField.queryCommandValue([&#39;boo&#39;, &#39;aieee&#39;]));
<a name="line922"></a>  editableField.dispose();
<a name="line923"></a>}
<a name="line924"></a>
<a name="line925"></a>
<a name="line926"></a>function testSetHtml() {
<a name="line927"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line928"></a>  var clock = new goog.testing.MockClock(true);
<a name="line929"></a>
<a name="line930"></a>  try {
<a name="line931"></a>    var delayedChangeCalled = false;
<a name="line932"></a>    goog.events.listen(editableField, goog.editor.Field.EventType.DELAYEDCHANGE,
<a name="line933"></a>        function() {
<a name="line934"></a>          delayedChangeCalled = true;
<a name="line935"></a>        });
<a name="line936"></a>
<a name="line937"></a>    editableField.makeEditable();
<a name="line938"></a>    clock.tick(1000);
<a name="line939"></a>    assertFalse(&#39;Make editable must not fire delayed change.&#39;,
<a name="line940"></a>        delayedChangeCalled);
<a name="line941"></a>
<a name="line942"></a>    editableField.setHtml(false, &#39;bar&#39;, true /* Don&#39;t fire delayed change */);
<a name="line943"></a>    goog.testing.dom.assertHtmlContentsMatch(&#39;bar&#39;, editableField.getElement());
<a name="line944"></a>    clock.tick(1000);
<a name="line945"></a>    assertFalse(&#39;setHtml must not fire delayed change if so configured.&#39;,
<a name="line946"></a>        delayedChangeCalled);
<a name="line947"></a>
<a name="line948"></a>    editableField.setHtml(false, &#39;foo&#39;, false /* Fire delayed change */);
<a name="line949"></a>    goog.testing.dom.assertHtmlContentsMatch(&#39;foo&#39;, editableField.getElement());
<a name="line950"></a>    clock.tick(1000);
<a name="line951"></a>    assertTrue(&#39;setHtml must fire delayed change by default&#39;,
<a name="line952"></a>        delayedChangeCalled);
<a name="line953"></a>  } finally {
<a name="line954"></a>    clock.dispose();
<a name="line955"></a>    editableField.dispose();
<a name="line956"></a>  }
<a name="line957"></a>}
<a name="line958"></a>
<a name="line959"></a>
<a name="line960"></a>/**
<a name="line961"></a> * Helper to test that the cursor is placed at the beginning of the editable
<a name="line962"></a> * field&#39;s contents.
<a name="line963"></a> * @param {string=} opt_html Html to replace the test file default field
<a name="line964"></a> *     contents with.
<a name="line965"></a> * @param {string=} opt_parentId Id of the parent of the node where the cursor
<a name="line966"></a> *     is expected to be placed. If omitted, will expect cursor to be placed in
<a name="line967"></a> *     the first child of the field element (or, if the field has no content, in
<a name="line968"></a> *     the field element itself).
<a name="line969"></a> */
<a name="line970"></a>function doTestPlaceCursorAtStart(opt_html, opt_parentId) {
<a name="line971"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line972"></a>  editableField.makeEditable();
<a name="line973"></a>  // Initially place selection not at the start of the editable field.
<a name="line974"></a>  var textNode = editableField.getElement().firstChild;
<a name="line975"></a>  goog.dom.Range.createFromNodes(textNode, 1, textNode, 2).select();
<a name="line976"></a>  if (opt_html != null) {
<a name="line977"></a>    editableField.getElement().innerHTML = opt_html;
<a name="line978"></a>  }
<a name="line979"></a>
<a name="line980"></a>  editableField.placeCursorAtStart();
<a name="line981"></a>  var range = editableField.getRange();
<a name="line982"></a>  assertNotNull(
<a name="line983"></a>      &#39;Placing the cursor should result in a range object being available&#39;,
<a name="line984"></a>      range);
<a name="line985"></a>  assertTrue(&#39;The range should be collapsed&#39;, range.isCollapsed());
<a name="line986"></a>  textNode = editableField.getElement().firstChild;
<a name="line987"></a>
<a name="line988"></a>  // We check whether getAttribute exist because textNode may be an actual
<a name="line989"></a>  // TextNode, which does not have getAttribute.
<a name="line990"></a>  if (textNode &amp;&amp; textNode.getAttribute &amp;&amp;
<a name="line991"></a>      textNode.getAttribute(&#39;_moz_editor_bogus_node&#39;)) {
<a name="line992"></a>    // At least in FF &gt;= 6, assigning &#39;&#39; to innerHTML of a contentEditable
<a name="line993"></a>    // element will results in textNode being modified into:
<a name="line994"></a>    // &lt;br _moz_editor_bogus_node=&quot;TRUE&quot; _moz_dirty=&quot;&quot;&gt; instead of nulling
<a name="line995"></a>    // it. So we should null it ourself.
<a name="line996"></a>    textNode = null;
<a name="line997"></a>  }
<a name="line998"></a>
<a name="line999"></a>  var startNode = opt_parentId ?
<a name="line1000"></a>      editableField.getEditableDomHelper().getElement(opt_parentId).firstChild :
<a name="line1001"></a>      textNode ? textNode : editableField.getElement();
<a name="line1002"></a>  if (goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersion(&#39;528&#39;)) {
<a name="line1003"></a>    // Safari 3 seems to normalize the selection to the shallowest endpoint (in
<a name="line1004"></a>    // this case the editable element) in all cases tested below. This is OK
<a name="line1005"></a>    // because when you start typing it magically inserts the text at the
<a name="line1006"></a>    // deepest endpoint, and even behaves as desired in the case tested by
<a name="line1007"></a>    // testPlaceCursorAtStartNonImportantTextNode.
<a name="line1008"></a>    startNode = editableField.getElement();
<a name="line1009"></a>  }
<a name="line1010"></a>  assertEquals(&#39;The range should start at the specified expected node&#39;,
<a name="line1011"></a>      startNode, range.getStartNode());
<a name="line1012"></a>  assertEquals(&#39;The range should start at the beginning of the node&#39;,
<a name="line1013"></a>      0, range.getStartOffset());
<a name="line1014"></a>}
<a name="line1015"></a>
<a name="line1016"></a>
<a name="line1017"></a>/**
<a name="line1018"></a> * Verify that restoreSavedRange() restores the range and sets the focus.
<a name="line1019"></a> */
<a name="line1020"></a>function testRestoreSavedRange() {
<a name="line1021"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1022"></a>  editableField.makeEditable();
<a name="line1023"></a>
<a name="line1024"></a>
<a name="line1025"></a>  // Create another node to take the focus later.
<a name="line1026"></a>  var doc = goog.dom.getOwnerDocument(editableField.getElement());
<a name="line1027"></a>  var otherElem = doc.createElement(&#39;div&#39;);
<a name="line1028"></a>  otherElem.tabIndex = &#39;1&#39;;  // Make it focusable.
<a name="line1029"></a>  editableField.getElement().parentNode.appendChild(otherElem);
<a name="line1030"></a>
<a name="line1031"></a>  // Initially place selection not at the start of the editable field.
<a name="line1032"></a>  var textNode = editableField.getElement().firstChild;
<a name="line1033"></a>  var range = goog.dom.Range.createFromNodes(textNode, 1, textNode, 2);
<a name="line1034"></a>  range.select();
<a name="line1035"></a>  var savedRange = goog.editor.range.saveUsingNormalizedCarets(range);
<a name="line1036"></a>
<a name="line1037"></a>  // Change range to be a simple cursor at start, and move focus away.
<a name="line1038"></a>  editableField.placeCursorAtStart();
<a name="line1039"></a>  otherElem.focus();
<a name="line1040"></a>
<a name="line1041"></a>  editableField.restoreSavedRange(savedRange);
<a name="line1042"></a>
<a name="line1043"></a>  // Verify that we have focus and the range is restored.
<a name="line1044"></a>  assertEquals(&#39;Field should be focused&#39;,
<a name="line1045"></a>      editableField.getElement(),
<a name="line1046"></a>      goog.dom.getActiveElement(doc));
<a name="line1047"></a>  var newRange = editableField.getRange();
<a name="line1048"></a>  assertEquals(&#39;Range startNode&#39;, textNode, newRange.getStartNode());
<a name="line1049"></a>  assertEquals(&#39;Range startOffset&#39;, 1, newRange.getStartOffset());
<a name="line1050"></a>  assertEquals(&#39;Range endNode&#39;, textNode, newRange.getEndNode());
<a name="line1051"></a>  assertEquals(&#39;Range endOffset&#39;, 2, newRange.getEndOffset());
<a name="line1052"></a>
<a name="line1053"></a>}
<a name="line1054"></a>
<a name="line1055"></a>
<a name="line1056"></a>function testPlaceCursorAtStart() {
<a name="line1057"></a>  doTestPlaceCursorAtStart();
<a name="line1058"></a>}
<a name="line1059"></a>
<a name="line1060"></a>
<a name="line1061"></a>function testPlaceCursorAtStartEmptyField() {
<a name="line1062"></a>  doTestPlaceCursorAtStart(&#39;&#39;);
<a name="line1063"></a>}
<a name="line1064"></a>
<a name="line1065"></a>
<a name="line1066"></a>function testPlaceCursorAtStartNonImportantTextNode() {
<a name="line1067"></a>  doTestPlaceCursorAtStart(
<a name="line1068"></a>      &#39;\n&lt;span id=&quot;important&quot;&gt;important text&lt;/span&gt;&#39;, &#39;important&#39;);
<a name="line1069"></a>}
<a name="line1070"></a>
<a name="line1071"></a>
<a name="line1072"></a>/**
<a name="line1073"></a> * Helper to test that the cursor is placed at the beginning of the editable
<a name="line1074"></a> * field&#39;s contents.
<a name="line1075"></a> * @param {string=} opt_html Html to replace the test file default field
<a name="line1076"></a> *     contents with.
<a name="line1077"></a> * @param {string=} opt_parentId Id of the parent of the node where the cursor
<a name="line1078"></a> *     is expected to be placed. If omitted, will expect cursor to be placed in
<a name="line1079"></a> *     the first child of the field element (or, if the field has no content, in
<a name="line1080"></a> *     the field element itself).
<a name="line1081"></a> * @param {number=} The offset to expect for the end position.
<a name="line1082"></a> */
<a name="line1083"></a>function doTestPlaceCursorAtEnd(opt_html, opt_parentId, opt_offset) {
<a name="line1084"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1085"></a>  editableField.makeEditable();
<a name="line1086"></a>
<a name="line1087"></a>  // Initially place selection not at the end of the editable field.
<a name="line1088"></a>  var textNode = editableField.getElement().firstChild;
<a name="line1089"></a>  goog.dom.Range.createFromNodes(textNode, 0, textNode, 1).select();
<a name="line1090"></a>  if (opt_html != null) {
<a name="line1091"></a>    editableField.getElement().innerHTML = opt_html;
<a name="line1092"></a>  }
<a name="line1093"></a>
<a name="line1094"></a>  editableField.placeCursorAtEnd();
<a name="line1095"></a>  var range = editableField.getRange();
<a name="line1096"></a>  assertNotNull(
<a name="line1097"></a>      &#39;Placing the cursor should result in a range object being available&#39;,
<a name="line1098"></a>      range);
<a name="line1099"></a>  assertTrue(&#39;The range should be collapsed&#39;, range.isCollapsed());
<a name="line1100"></a>  textNode = editableField.getElement().firstChild;
<a name="line1101"></a>
<a name="line1102"></a>  // We check whether getAttribute exist because textNode may be an actual
<a name="line1103"></a>  // TextNode, which does not have getAttribute.
<a name="line1104"></a>
<a name="line1105"></a>  var hasBogusNode = textNode &amp;&amp; textNode.getAttribute &amp;&amp;
<a name="line1106"></a>                     textNode.getAttribute(&#39;_moz_editor_bogus_node&#39;);
<a name="line1107"></a>  if (hasBogusNode) {
<a name="line1108"></a>    // At least in FF &gt;= 6, assigning &#39;&#39; to innerHTML of a contentEditable
<a name="line1109"></a>    // element will results in textNode being modified into:
<a name="line1110"></a>    // &lt;br _moz_editor_bogus_node=&quot;TRUE&quot; _moz_dirty=&quot;&quot;&gt; instead of nulling
<a name="line1111"></a>    // it. So we should null it ourself.
<a name="line1112"></a>    textNode = null;
<a name="line1113"></a>  }
<a name="line1114"></a>
<a name="line1115"></a>  var endNode = opt_parentId ?
<a name="line1116"></a>      editableField.getEditableDomHelper().getElement(opt_parentId).lastChild :
<a name="line1117"></a>      textNode ? textNode : editableField.getElement();
<a name="line1118"></a>  assertEquals(&#39;The range should end at the specified expected node&#39;,
<a name="line1119"></a>      endNode, range.getEndNode());
<a name="line1120"></a>  var offset = goog.isDefAndNotNull(opt_offset) ?
<a name="line1121"></a>      opt_offset :
<a name="line1122"></a>      textNode ? endNode.nodeValue.length : endNode.childNodes.length - 1;
<a name="line1123"></a>  if (hasBogusNode) {
<a name="line1124"></a>    assertEquals(&#39;The range should end at the ending of the bogus node &#39; +
<a name="line1125"></a>                 &#39;added by FF&#39;, offset + 1, range.getEndOffset());
<a name="line1126"></a>  } else {
<a name="line1127"></a>    assertEquals(&#39;The range should end at the ending of the node&#39;,
<a name="line1128"></a>        offset, range.getEndOffset());
<a name="line1129"></a>  }
<a name="line1130"></a>}
<a name="line1131"></a>
<a name="line1132"></a>
<a name="line1133"></a>function testPlaceCursorAtEnd() {
<a name="line1134"></a>  doTestPlaceCursorAtEnd();
<a name="line1135"></a>}
<a name="line1136"></a>
<a name="line1137"></a>
<a name="line1138"></a>function testPlaceCursorAtEndEmptyField() {
<a name="line1139"></a>  doTestPlaceCursorAtEnd(&#39;&#39;, null, 0);
<a name="line1140"></a>}
<a name="line1141"></a>
<a name="line1142"></a>
<a name="line1143"></a>function testPlaceCursorAtEndNonImportantTextNode() {
<a name="line1144"></a>  doTestPlaceCursorAtStart(
<a name="line1145"></a>      &#39;\n&lt;span id=&quot;important&quot;&gt;important text&lt;/span&gt;&#39;, &#39;important&#39;);
<a name="line1146"></a>}
<a name="line1147"></a>
<a name="line1148"></a>
<a name="line1149"></a>// Tests related to change/delayed change events.
<a name="line1150"></a>
<a name="line1151"></a>
<a name="line1152"></a>function testClearDelayedChange() {
<a name="line1153"></a>  var clock = new goog.testing.MockClock(true);
<a name="line1154"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1155"></a>  editableField.makeEditable();
<a name="line1156"></a>
<a name="line1157"></a>  var delayedChangeCalled = false;
<a name="line1158"></a>  goog.events.listen(editableField, goog.editor.Field.EventType.DELAYEDCHANGE,
<a name="line1159"></a>      function() {
<a name="line1160"></a>        delayedChangeCalled = true;
<a name="line1161"></a>      });
<a name="line1162"></a>
<a name="line1163"></a>  // Clears delayed change timer.
<a name="line1164"></a>  editableField.delayedChangeTimer_.start();
<a name="line1165"></a>  editableField.clearDelayedChange();
<a name="line1166"></a>  assertTrue(delayedChangeCalled);
<a name="line1167"></a>  if (editableField.changeTimerGecko_) {
<a name="line1168"></a>    assertFalse(editableField.changeTimerGecko_.isActive());
<a name="line1169"></a>  }
<a name="line1170"></a>  assertFalse(editableField.delayedChangeTimer_.isActive());
<a name="line1171"></a>
<a name="line1172"></a>  // Clears delayed changes caused by changeTimerGecko_
<a name="line1173"></a>  if (editableField.changeTimerGecko_) {
<a name="line1174"></a>    delayedChangeCalled = false;
<a name="line1175"></a>    editableField.changeTimerGecko_.start();
<a name="line1176"></a>    editableField.clearDelayedChange();
<a name="line1177"></a>    assertTrue(delayedChangeCalled);
<a name="line1178"></a>    if (editableField.changeTimerGecko_) {
<a name="line1179"></a>      assertFalse(editableField.changeTimerGecko_.isActive());
<a name="line1180"></a>    }
<a name="line1181"></a>    assertFalse(editableField.delayedChangeTimer_.isActive());
<a name="line1182"></a>  }
<a name="line1183"></a>  clock.dispose();
<a name="line1184"></a>}
<a name="line1185"></a>
<a name="line1186"></a>
<a name="line1187"></a>function testHandleChange() {
<a name="line1188"></a>  if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
<a name="line1189"></a>    var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1190"></a>    editableField.makeEditable();
<a name="line1191"></a>
<a name="line1192"></a>    editableField.changeTimerGecko_.start();
<a name="line1193"></a>    editableField.handleChange();
<a name="line1194"></a>    assertFalse(editableField.changeTimerGecko_.isActive());
<a name="line1195"></a>  }
<a name="line1196"></a>}
<a name="line1197"></a>
<a name="line1198"></a>
<a name="line1199"></a>function testDispatchDelayedChange() {
<a name="line1200"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1201"></a>  editableField.makeEditable();
<a name="line1202"></a>
<a name="line1203"></a>  editableField.delayedChangeTimer_.start();
<a name="line1204"></a>  editableField.dispatchDelayedChange_();
<a name="line1205"></a>  assertFalse(editableField.delayedChangeTimer_.isActive());
<a name="line1206"></a>}
<a name="line1207"></a>
<a name="line1208"></a>
<a name="line1209"></a>function testHandleWindowLevelMouseUp() {
<a name="line1210"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1211"></a>  if (editableField.usesIframe()) {
<a name="line1212"></a>    // Only run this test if the editor does not use an iframe.
<a name="line1213"></a>    return;
<a name="line1214"></a>  }
<a name="line1215"></a>  editableField.setUseWindowMouseUp(true);
<a name="line1216"></a>  editableField.makeEditable();
<a name="line1217"></a>  var selectionHasFired = false;
<a name="line1218"></a>  goog.events.listenOnce(
<a name="line1219"></a>      editableField, goog.editor.Field.EventType.SELECTIONCHANGE,
<a name="line1220"></a>      function(e) {
<a name="line1221"></a>        selectionHasFired = true;
<a name="line1222"></a>      });
<a name="line1223"></a>  var editableElement = editableField.getElement();
<a name="line1224"></a>  var otherElement = goog.dom.createDom(&#39;div&#39;);
<a name="line1225"></a>  goog.dom.insertSiblingAfter(otherElement, document.body.lastChild);
<a name="line1226"></a>
<a name="line1227"></a>  goog.testing.events.fireMouseDownEvent(editableElement);
<a name="line1228"></a>  assertFalse(selectionHasFired);
<a name="line1229"></a>  goog.testing.events.fireMouseUpEvent(otherElement);
<a name="line1230"></a>  assertTrue(selectionHasFired);
<a name="line1231"></a>}
<a name="line1232"></a>
<a name="line1233"></a>function testNoHandleWindowLevelMouseUp() {
<a name="line1234"></a>  var editableField = new FieldConstructor(&#39;testField&#39;, document);
<a name="line1235"></a>  editableField.setUseWindowMouseUp(false);
<a name="line1236"></a>  editableField.makeEditable();
<a name="line1237"></a>  var selectionHasFired = false;
<a name="line1238"></a>  goog.events.listenOnce(
<a name="line1239"></a>      editableField, goog.editor.Field.EventType.SELECTIONCHANGE,
<a name="line1240"></a>      function(e) {
<a name="line1241"></a>        selectionHasFired = true;
<a name="line1242"></a>      });
<a name="line1243"></a>  var editableElement = editableField.getElement();
<a name="line1244"></a>  var otherElement = goog.dom.createDom(&#39;div&#39;);
<a name="line1245"></a>  goog.dom.insertSiblingAfter(otherElement, document.body.lastChild);
<a name="line1246"></a>
<a name="line1247"></a>  goog.testing.events.fireMouseDownEvent(editableElement);
<a name="line1248"></a>  assertFalse(selectionHasFired);
<a name="line1249"></a>  goog.testing.events.fireMouseUpEvent(otherElement);
<a name="line1250"></a>  assertFalse(selectionHasFired);
<a name="line1251"></a>}
</pre>


</body>
</html>
